import { defineStore } from "pinia";
import { getPlayListCategoryDict } from "@/api/album";

const useCommonStore = defineStore("common", {
  state: (): {
    activedMenu: string;
    albumPageId: number | string | null;
    playListCategoryDict: { [key: number]: string };
  } => {
    const storedId = window.localStorage.getItem("albumPageId");
    return {
      activedMenu: "home",
      albumPageId: typeof storedId !== "undefined" ? storedId : -1,
      playListCategoryDict: {},
    };
  },

  actions: {
    switchActivedMenu(payload: string) {
      this.activedMenu = payload;
    },
    setAlbumPageId(payload: number | string) {
      window.localStorage.setItem("albumPageId", String(payload));
      this.albumPageId = payload;
    },
    async updatePlaylistCategoryDict() {
      const data = await getPlayListCategoryDict();

      if (data.code === 200) {
        this.playListCategoryDict = data.categories;
      }
    },
  },
});

export default useCommonStore;
